#include <iostream>
#include "globals.h"
#include "Titleset.h"

Titleset::Titleset(const std::string &sfile) {
	file=sfile;
	ox=0.0;
	oy=0.0;
	mask=0;
}

Titleset::Titleset(const char* cfile) {
	file=cfile;
	ox=0.0;
	oy=0.0;
	mask=0;
}

Titleset::~Titleset() {

}

bool Titleset::read_sprite() {
	string file2=file+".titleset";
	inputStream.open(file2.c_str(),ios::in);
	if( !inputStream ) {
		cerr << "Error opening " << file2 << " ." << endl;
		if(!file_exist(file2.c_str())) {
			cerr << "file doesn't exist !" << endl;
		}
		inputStream.close();
		return false;
	}
	string line="";
	int lines;
	inputStream >> line;
	if( line.compare("TITLESETv3")!=0 ) {
		cerr << "Wrong titleset version" << endl;
		inputStream.close();
		return false;
	}
	inputStream >> lines;//number of titles;
	sf::Rect<int> rect;
	sf::Sprite sprite;
	string name;
	if(lines>0) {
		titles.clear();
		//titles.swap(spmap());
	}
	for(int i=0;i<lines;i++) {
		inputStream >> rect.Left >> rect.Top >> rect.Right >> rect.Bottom;
		rect.Right+=rect.Left;
		rect.Bottom+=rect.Top;
		inputStream >> name;
		//cout << "name = " << name << "\t" << rect.Left << "\t" << rect.Top << "\t" << rect.Right << "\t" << rect.Bottom << "\n";
		pair<spmapit,bool> p;
		p=titles.insert( make_pair( name, sprite ) );
		if ( p.second ) {
			p.first->second.SetImage(image);
			p.first->second.SetSubRect(rect);
			/*sf::Sprite *sp=&(*p.first).second;
			sp->SetImage(image);
			sp->SetSubRect(rect);*/
			//cout << "title inserted : " << name << endl;
			//cout << "size= " << p.first->second.GetSubRect().Right-p.first->second.GetSubRect().Left << "\t" << p.first->second.GetSubRect().Bottom-p.first->second.GetSubRect().Top << endl;
		}
		else {
			cout << "title already inserted : " << name << endl;
		}
	}
	inputStream >> lines;//number of sprite to draw
	int x,y;
	unsigned int couche,override;
	if(lines>0) {
		layers.clear();
		//layers.swap(splay());
		layers_over.clear();
		//layers_over.swap(splay());
	}
	for(int i=0;i<lines;i++) {
		inputStream >> x >> y >> couche >> override >> name;
		/*while(layers.size()<=couche) {
			layers.push_back(vector<sf::Sprite>());
			layers_over.push_back(vector<sf::Sprite>());
		}*/
		if(layers.size()<=couche) {
			layers.resize(couche+1,vector<sf::Sprite>());
			layers_over.resize(couche+1,vector<sf::Sprite>());
		}
		//cout << x << "\t" << y << "\t" << couche << "\t" << override << "\t" << name << endl;
		layers.at(couche).push_back(titles.at(name));
		layers.at(couche).back().SetPosition((float)x+ox,(float)y+oy);
		//cout << layers.at(couche).back().GetPosition().x << "\t" << layers.at(couche).back().GetPosition().y << "\t" << layers.at(couche).back().GetSize().x << "\t" << layers.at(couche).back().GetSize().y << "\t" << name << endl;
		if(override==1) {
			layers_over.at(couche).push_back(layers.at(couche).back());
		}
	}
	inputStream >> line;
	if( line.compare("end")!=0 ) {
		cerr << "Wrong titleset end" << endl;
		inputStream.close();
		return false;
	}
	fflush(stdout);
	inputStream.close();
	return true;
}

bool Titleset::init() {
	if (!image.LoadFromFile(file)) {
		printf ("ERROR: Did not found the File %s !\n",file.c_str());
		
		if(!file_exist(file.c_str())) {
			cerr << "file doesn't exist !" << endl;
		}
		return false;
	}
	return read_sprite();
}

void Titleset::set_shift(float o_x,float o_y) {
	ox=o_x;
	oy=o_y;
}

sf::Vector2f Titleset::get_shift() const {
	return sf::Vector2f(ox,oy);
}

void Titleset::draw_layer(unsigned int l) const {
	for( spvecitc it=layers.at(l).begin(); it!=layers.at(l).end(); it++ ) {
		/*cout << it->GetPosition().x << "\t" << it->GetPosition().y << "\n";
		cout << it->GetSubRect().Right-it->GetSubRect().Left << "\t" << it->GetSubRect().Bottom-it->GetSubRect().Top << endl;*/
		App.Draw(*it);
	}
}

void Titleset::draw() const {
	unsigned int _m=1;
	for(unsigned int i=0;i<layers.size();i++) {
		if( (mask&_m) == _m ) draw_layer(i);
		_m<<=1;
	}
}

void Titleset::show_layer(unsigned int l) {
	unsigned int _m=1;
	_m<<=l;
	mask|=_m;
}

void Titleset::hide_layer(unsigned int l) {
	unsigned int _m=1;
	_m<<=l;
	mask&=!_m;
}

void Titleset::chg_layer(unsigned int l) {
	unsigned int _m=1;
	_m<<=l;
	mask^=_m;
}
